<template>
  <div class="login">
    <el-container>
      <el-header>登录</el-header>
      <el-main>
        <el-form ref="formdata1" :rules="rules" :model="formdata">
          <el-form-item label="手机号" prop="mobile">
            <el-input
              placeholder="请输入手机号"
              v-model="formdata.mobile"
              maxlength="11"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              placeholder="请输入密码"
              type="password"
              v-model="formdata.password"
              maxlength="11"
            >
            </el-input>
          </el-form-item>

          <el-form-item  prop="password">
              <el-button @click="submitForm" type='primary'>登录</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import {userLogin}  from '@/api/api'
export default {
  props: [],
  components: {},
  data() {
    return {
      formdata: {
        mobile: "",
        password: "",
      },
    //   这里是表单  父子组件传值的 校验数据 
      rules:{
          mobile:[
              {
                  required:true,
                  message:'请输入手机号',
                  trigger:'blur'
              },
              {
                  pattern:/^1[3-9]\d{9}$/,
                  message:'手机号格式不正确',
                  trigger:'blur'
              }
          ],
          password:[
                {
                  required:true,
                  message:'请输入password',
                  trigger:'blur'
              },
              {
                  pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/,
                  message:'密码不正确',
                  trigger:'blur'
              }
          ]
      }

    };
  },

  methods: {
      submitForm(){
          console.log('submitform')
          this.$refs.formdata1.validate(async (valid)=>{
              console.log('valid',valid)
              if(valid) {
                  console.log('valid true 开始核心逻辑') 
                  let ret = await userLogin(this.formdata)
                  console.log('ret',ret)
                  this.$store.commit('usermobile',ret.data.mobile)
                   this.$store.commit('tokenadd',ret.data.data._token)
                   localStorage.setItem('_token',ret.data.data._token)


                   this.$message({
                       message:'准备个人中心',
                       type:'success'
                   })

                   setTimeout(()=>{
                       this.$router.push('/center')
                   },2000)
              }
          })
      }
  },

  computed: {},

  watch: {},

  filters: {},

  beforeCreate() {},

  created() {
    setTimeout(() => {
      this.eventbus.$emit("footnav", false);
    }, 0);
  },

  beforeMount() {},

  mounted() {},

  beforeUpdate() {},

  updated() {},

  beforeDestroy() {},

  destroyed() {
    setTimeout(() => {
      this.eventbus.$emit("footnav", true);
    }, 0);
  },
};
</script>


<style  lang='scss'>
.login {
  padding-top: 80px;
  .el-container {
    width: 80%;
    margin-left: 40px;
    .el-header {
      font-size: 23px;
    }
  }
}
</style>
